package GUI;

import Parser.*;
import DecisionMaker.*;
import java.awt.*;
import javax.swing.*;

public class Tank extends JPanel implements Process
{
  //attributen
  public double height =100.0;
  public double influx = 0.0;
  public double outflux = 8.0;
  private KBParser parser;
  private DecisionMaker DMMaker;

  //constructor
  public Tank (KBParser prsr)
  {
    parser = prsr;
  }

  //methods
  protected void paintComponent (Graphics g)
  {
    super.paintComponent(g);
    setBackground(Color.WHITE);
    g.setColor(getForeground());
    DrawTank(g);
    DrawHeightLine(g, getVariable("height"));
    simulateStep(0.1);
  }

  private void DrawTank (Graphics g)
  {
    g.setColor(Color.BLACK);
    g.drawLine(160, 210, 280, 210);
    g.drawLine(160, 210, 160, 220);
    g.drawLine(160, 220, 170, 220);
    g.drawLine(170, 220, 170, 370);
    g.drawLine(170, 370, 300, 370);
    g.drawLine(300, 370, 300, 360);
    g.drawLine(300, 360, 280, 360);
    g.drawLine(280, 360, 280, 210);

    g.drawString("OutFlux = " + getVariable("outflux"), 180, 400);
    g.drawString("InFlux  = " + getVariable("inFlux"), 180, 425);
    g.drawString("Height  = " + getVariable("height"), 180, 450);
  }

  private void DrawHeightLine (Graphics g, double dHeight)
  {
    dHeight = 360 - dHeight;
    g.setColor(Color.RED);
    g.drawLine(170, (int) dHeight, 280, (int) dHeight);
  }

  public double getVariable (String name)
  {
    if (name.equals("inFlux"))
    {
      return influx;
    }
    if (name.equals("outflux"))
    {
      return outflux;
    }
    if (name.equals("height"))
    {
      return height;
    }
    return 0.0;
  }

  public void setVariable (String name, double value)
  {
    if (name.equals("inFlux"))
    {
      this.influx = value;
    }
    if (name.equals("outflux"))
    {
      this.outflux = value;
    }
    if (name.equals("height"))
    {
        this.height = value;
    }
  }

  public void simulateStep (double deltaT)
  {

    DMMaker = new DecisionMaker(parser, this);
    DMMaker.ApplyRules();
    //System.out.println("**INFLUX is: " + this.influx + "**");
    //System.out.println("**OUTFLUX is: " + this.outflux + "**");
    //System.out.println("**HEIGHT voor optelling is: " + this.height  + "**");

    setVariable("height", (height + ((influx - outflux) * deltaT)));
    //System.out.println("**HEIGHT na optelling is: " + this.height + "**");

    repaint();
  }

  public String getProcessName()
  {
      return "Tank";
  }
}
